package com.anysoft.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.anysoft.R;

public class LoadingDialog extends Dialog {

    private SimpleDraweeView sdv_loading;

    private View mDialogView;

    private AnimationSet mAnimIn, mAnimOut;

    private OnNegativeListener mNegativeListener;

    private boolean mIsShowAnim;

    private Context context;

    public LoadingDialog(Context context) {
        super(context);
        this.context = context;
        init();
    }
//    public LoadingDialog(Context context) {
//        this(context, 0);
//    }
//
//    public LoadingDialog(Context context, int theme) {
//        super(context, R.style.color_loading_dialog);
//        this.context = context;
//        init();
//    }

    private void callDismiss() {
        super.dismiss();
    }

    private void init() {
        mAnimIn = AnimationLoader.getInAnimation(getContext());
        mAnimOut = AnimationLoader.getOutAnimation(getContext());
        initAnimListener();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // 这句代码换掉dialog默认背景，否则dialog的边缘发虚透明而且很宽
        // 总之达不到想要的效果
        getWindow().setBackgroundDrawableResource(R.color.translucent_white_60);

        View contentView = View.inflate(getContext(), R.layout.dialog_loading, null);
        setContentView(contentView);

        // 这句话起全屏的作用
        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);

        mDialogView = getWindow().getDecorView().findViewById(android.R.id.content);
        sdv_loading = (SimpleDraweeView) contentView.findViewById(R.id.sdv_loading);

        /**
         * 下面是主要代码：
         */
        DraweeController draweeController = Fresco.newDraweeControllerBuilder()
                .setAutoPlayAnimations(true)//自动播放动画
//                .setUri(Uri.parse("asset://com.anysoft/welcome.gif"))//路径
                .setUri(Uri.parse("asset://com.anysoft/loading.gif"))//路径
                .build();
        sdv_loading.setController(draweeController);
    }

    @Override
    protected void onStart() {
        super.onStart();
        startWithAnimation(mIsShowAnim);
    }

    @Override
    public void dismiss() {
        dismissWithAnimation(mIsShowAnim);
    }

    private void startWithAnimation(boolean showInAnimation) {
        if (showInAnimation) {
            mDialogView.startAnimation(mAnimIn);
        }
    }

    private void dismissWithAnimation(boolean showOutAnimation) {
        if (showOutAnimation) {
            mDialogView.startAnimation(mAnimOut);
        } else {
            super.dismiss();
        }
    }

    private void initAnimListener() {
        mAnimOut.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mDialogView.post(new Runnable() {
                    @Override
                    public void run() {
                        callDismiss();
                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }

    public LoadingDialog setAnimationEnable(boolean enable) {
        mIsShowAnim = enable;
        return this;
    }

    public LoadingDialog setAnimationIn(AnimationSet animIn) {
        mAnimIn = animIn;
        return this;
    }

    public LoadingDialog setAnimationOut(AnimationSet animOut) {
        mAnimOut = animOut;
        initAnimListener();
        return this;
    }

    public interface OnNegativeListener {
        void onClick(LoadingDialog dialog);
    }

    public LoadingDialog setNegativeListener(OnNegativeListener l) {
        mNegativeListener = l;
        return this;
    }

}
